#!/usr/bin/python
# -*- coding: UTF-8 -*-
from aip import AipSpeech
import speech_recognition as sr
import pocketsphinx
import config.config as conf

#从百度AI开放平台创建应用处获取
config_info = conf.read_conf()
APP_ID = config_info["BAIDU_APP_ID"]
API_KEY = config_info["BAIDU_APP_KEY"]
SECRET_KEY = config_info["BAIDU_SECRET_KEY"]

client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

# 识别本地文件
def get_text(audio):
    result = client.asr(audio, 'wav', 16000, {
    'dev_pid': 1536,})
    print(result)
    text = result['result'][0]
    return text

# 进行离线语音识别
def get_text_offline(audio):
    r = sr.Recognizer()
    print(r)
    try:
        text = r.recognize_sphinx(audio)
        print(text)
        return text
    except sr.UnknownValueError:
        print("Sphinx could not understand audio")
    except sr.RequestError as e:
        print("Sphinx error; {0}".format(e))

if __name__ == '__main__':
    get_text()
    print(get_text())
